package com.bhs.sansonglogistics.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.nirvana.tools.core.AppUtils;

//行为分 仪表盘
public class ArcProgressBar extends View {


    // 1.创建一个画笔
    private final Paint mPaint = new Paint();//背景
    private final Paint mPaint2 = new Paint();//进度
    private final Paint mPaint3 = new Paint();//文字
    private float schedule = 0;//进度


    public ArcProgressBar(Context context) {
        super(context);
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    // 2.初始化画笔
    private void initPaint() {
        mPaint.setColor(Color.parseColor("#F3F4F5"));       //设置画笔颜色
        mPaint.setStyle(Paint.Style.STROKE);  //设置画笔模式为填充
        mPaint.setStrokeWidth(70f);         //设置画笔宽度为10px
        mPaint.setStrokeCap(Paint.Cap.ROUND);//设置圆头线帽
        mPaint.setAntiAlias(true);

        mPaint2.setColor(Color.parseColor("#485EF4"));       //设置画笔颜色
        mPaint2.setStyle(Paint.Style.STROKE);  //设置画笔模式为填充
        mPaint2.setStrokeWidth(70f);         //设置画笔宽度为10px
        mPaint2.setStrokeCap(Paint.Cap.ROUND);//设置圆头线帽
        mPaint2.setAntiAlias(true);

        mPaint3.setColor(Color.parseColor("#909399"));       //设置画笔颜色
        mPaint3.setStyle(Paint.Style.FILL);  //设置画笔模式为填充
        mPaint3.setStrokeWidth(4f);         //设置画笔宽度为10px
        mPaint3.setTextSize(28);
    }

    public void setProgressBar(float schedule) {
        this.schedule = schedule;
        invalidate();
    }


    private int width, height;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式

        height = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式

        LogUtils.d(width + "==" + widthMode);
        LogUtils.d(height + "==" + heightMode);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        LogUtils.d("onSizeChanged" + w + "==" + h);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        LogUtils.d("onLayout" + left + "==" + top + "==" + right + "==" + bottom);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        int startValue = 150;
        int endValue = 240;
        RectF rectF = new RectF(50, 50, width - 50, height - 50);
        RectF rectF2 = new RectF(120, 120, width - 120, height - 120);

        // 绘制圆弧背景
        canvas.drawArc(rectF, startValue, endValue, false, mPaint);
        // 绘制圆弧进度
        canvas.drawArc(rectF, startValue, schedule * 3 - (360- endValue), false, mPaint2);

        //8个刻度
        for (int i = 0; i <= 8; i++) {
            Path path = new Path();
            path.addArc(rectF2, (float) (startValue + i * 30), endValue);
            if ((i % 2) == 1) {
                canvas.drawTextOnPath(MyUtils.setRanking(i), path, 0f, 0f, mPaint3);
            } else {
                canvas.drawTextOnPath(String.valueOf(40 + i * 10), path, 0f, 0f, mPaint3);
            }
        }
    }
}
